#!/bin/bash
set -e

result_path=""

local_dir=$(echo ${JIANMU_LOCAL_PATH%/*})
local_name=$(echo ${JIANMU_LOCAL_PATH##*/})

remote_dirs=$(echo ${JIANMU_REMOTE_PATH} | tr '/' ' ')
remote_dir=""
remote_path=$(echo ${JIANMU_REMOTE_PATH%/*})
remote_name=$(echo ${JIANMU_REMOTE_PATH##*/})

if [[ ${JIANMU_COMMAND} == "put" ]]; then
  find ${JIANMU_LOCAL_PATH}/ -name ".svn" | xargs rm -rf
  for dir in ${remote_dirs}
  do
    remote_dir="${remote_dir}${dir}/"
    smbclient ${JIANMU_SHARE_URL} -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "mkdir ${remote_dir}"
  done
  command="put"
  if [[ -z ${local_name} ]]; then
    command="prompt OFF; recurse ON; mput *"
  fi
  smbclient ${JIANMU_SHARE_URL} -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "lcd ${local_dir}/; cd ${JIANMU_REMOTE_PATH}; ${command} ${local_name}"
  result_path=${JIANMU_SHARE_URL}/${JIANMU_REMOTE_PATH}/${local_name}
elif [[ ${JIANMU_COMMAND} == "get" ]]; then
  mkdir -p ${JIANMU_LOCAL_PATH}
  command="get"
  if [[ -z ${remote_name} ]]; then
    command="prompt OFF; recurse ON; mget *"
  fi
  smbclient ${JIANMU_SHARE_URL} -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "lcd ${JIANMU_LOCAL_PATH}/; cd ${remote_path}; ${command} ${remote_name}"
  result_path=${JIANMU_LOCAL_PATH}/${remote_name}
elif [[ ${JIANMU_COMMAND} == "mkdir" ]]; then
  for dir in ${remote_dirs}
  do
    remote_dir="${remote_dir}${dir}/"
    smbclient ${JIANMU_SHARE_URL} -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "mkdir ${remote_dir}"
  done
  result_path=${JIANMU_SHARE_URL}/${JIANMU_REMOTE_PATH}
elif [[ ${JIANMU_COMMAND} == "rmdir" ]]; then
  smbclient ${JIANMU_SHARE_URL} -D "${remote_path}" -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "recurse on; ls ${remote_name}" | grep '^\\' \
   | awk -F\\ '{print NF,$0}' | sort -nr | cut -d ' ' -f2- \
   | while read -r myDir; do
      myRoot="${myDir%\\*}"
      myFolder="${myDir##*\\}"
      smbclient ${JIANMU_SHARE_URL} -D "${myDir}" -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "rm ./*" || true
      smbclient ${JIANMU_SHARE_URL} -D "${myRoot}" -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "rmdir ${myFolder}" || true
  done
  result_path=${JIANMU_SHARE_URL}/${JIANMU_REMOTE_PATH}
elif [[ ${JIANMU_COMMAND} == "rm" ]]; then
  smbclient ${JIANMU_SHARE_URL} -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "cd ${remote_path}; rm ${remote_name}" 2>/dev/null || :
  result_path=${JIANMU_SHARE_URL}/${JIANMU_REMOTE_PATH}
elif [[ ${JIANMU_COMMAND} == "ls" ]]; then
  smbclient ${JIANMU_SHARE_URL} -D "${remote_path}" -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "recurse on; ls ${remote_name}"
  result_path=$(smbclient ${JIANMU_SHARE_URL} -D "${remote_path}" -U ${JIANMU_SMB_USERNAME}%${JIANMU_SMB_PASSWORD} -c "recurse on; ls ${remote_name}" | awk 'END {print}' | awk '$1=$1')
else
  echo "[ERROR] 当前操作类型只支持put、get、mkdir、rmdir、rm、ls，请检查！"
  exit 1
fi

echo "resultFile:"
echo -e "{\"result_path\":\"${result_path}\"}" > resultFile
mv resultFile /usr

cat /usr/resultFile